<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">医生名字</label>
        <el-input v-model="query.nickName" clearable placeholder="医生名字" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">职称</label>
        <el-input v-model="query.professionalTitle" clearable placeholder="职称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="650px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="未分配的医生" prop="doctorId">
            <el-select
              v-model="form.doctorId"
              filterable
              style="width: 370px"
              placeholder="请选择"
              :disabled="crud.status.title=='编辑医生管理'"
            >
              <el-option
                v-for="item in doctors"
                :key="item.id"
                :label="item.nickName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="分配科室" prop="departmentId">
            <tree-select
              v-model="form.departmentId"
              :options="departments"
              :load-options="loadDepartment"
              style="width: 370px;"
              placeholder="选择科室"
            />
          </el-form-item>
          <el-form-item label="职称">
            <el-input v-model="form.professionalTitle" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="专长领域">
            <el-input v-model="form.specialty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="排班时间间隔（分钟）一个号" prop="intervalMinutes" style="width: 400px">
            <el-input-number v-model="form.intervalMinutes" :min="1" :max="60" />
          </el-form-item>
          <el-form-item label="挂号费" prop="fee" style="width: 400px">
            <el-input-number v-model="form.fee" :min="1" :max="60" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="doctorId" label="医生ID" />
        <el-table-column prop="nickName" label="医生姓名" />
        <el-table-column prop="departmentName" label="科室名称" />
        <el-table-column prop="fee" label="挂号费" />
        <el-table-column prop="intervalMinutes" label="排号间隔时间" />
        <el-table-column prop="professionalTitle" label="职称" />
        <el-table-column prop="specialty" label="专长领域" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column v-if="checkPer(['admin','sysDoctor:edit','sysDoctor:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSysDoctor from '@/api/system/sysDoctor'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation.vue'
import crudOperation from '@crud/CRUD.operation.vue'
import udOperation from '@crud/UD.operation.vue'
import pagination from '@crud/Pagination.vue'
import crudSysDepartment from '@/api/system/sysDepartment'
import TreeSelect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import { getNoIsDoctorUsers } from '@/api/system/user'
const defaultForm = { doctorId: null, departmentId: null, professionalTitle: null, practiceLicenseNumber: null, specialty: null, createTime: null, updateTime: null }
export default {
  name: 'SysDoctor',
  components: { pagination, crudOperation, rrOperation, udOperation, TreeSelect },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '医生管理', url: 'api/sysDoctor', idField: 'doctorId', sort: 'doctorId,desc', crudMethod: { ...crudSysDoctor }})
  },
  data() {
    return {
      doctors: [],
      departments: [],
      permission: {
        add: ['admin', 'sysDoctor:add'],
        edit: ['admin', 'sysDoctor:edit'],
        del: ['admin', 'sysDoctor:del']
      },
      rules: {
        doctorId: [
          { required: true, message: '医生ID不能为空', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '科室ID不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'professionalTitle', display_name: '职称' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      console.log(form)
      this.getUsers()
      this.departments = []
      if (form.id != null) {
        if (form.parent_id === null) {
          form.parentId = 0
        }
        this.getSupDepts(form.id)
      } else {
        this.departments.push({ id: 0, label: '顶级科室', children: null })
        this.form.departmentId = form.departmentId
        this.form.departmentName = form.departmentName
      }
      this.form.intervalMinutes = 15
      // this.form.fee = 10
      // this.getDoctors()
    },
    loadDepartment({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        crudSysDepartment.getDepartmentsTree(parentNode.id).then(res => {
          // 遍历res 然后把res的name赋值成每个值的label
          res.forEach(item => {
            item.label = item.name
            item.id = item.departmentId
            if (item.subCount === 0) {
              item.hasChildren = true
              item.leaf = true
            } else {
              item.hasChildren = false
              item.leaf = false
            }
          })
          parentNode.children = res.map(function(obj) {
            if (!obj.leaf) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 100)
        })
      }
    },
    getUsers() {
      getNoIsDoctorUsers().then(res => {
        this.doctors = res
      }).catch(e => {
        console.log(e)
      })
    }

  }
}
</script>

<style scoped>

</style>
